package com.eedi.framework.org.controller.sys;

import com.eedi.framework.org.controller.sys.vo.config.*;
import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.org.controller.sys.vo.config.*;
import com.eedi.framework.org.service.SysOrgConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.Comparator;
import java.util.List;

import static com.eedi.framework.common.pojo.CommonResult.success;

@Tag(name = "系统界面 - 机构通用配置")
@RestController
@RequestMapping("/sys-org-config")
@Validated
@Slf4j
public class SysOrgConfigController {

    @Resource
    SysOrgConfigService sysOrgConfigService;

    @GetMapping("/basic/get")
    @Operation(summary = "获取机构通用配置基本信息")
    @PreAuthorize("@sys.hasPermission('sys:org-config:query')")
    public CommonResult<SysOrgConfigResp> get() {
        return success(sysOrgConfigService.get());
    }

    @PostMapping("/basic/setup")
    @Operation(summary = "设置机构通用配置基本信息")
    @PreAuthorize("@sys.hasPermission('sys:org-config:create')")
    public CommonResult<Void> setup(@RequestBody SysOrgConfigReq req) {
        sysOrgConfigService.setup(req);
        return success(null);
    }

    @PostMapping("/placard/create")
    @Operation(summary = "创建机构配置信息-海报配置")
    @PreAuthorize("@sys.hasPermission('sys:org-config-placard:create')")
    public CommonResult<String> createPlacard(@Valid @RequestBody SysOrgConfigPlacardReq createReqVO) {
        String orgConfigPlacardId = sysOrgConfigService.createPlacard(createReqVO);
        return success(orgConfigPlacardId);
    }

    @PutMapping("/placard/update")
    @Operation(summary = "修改机构配置信息-海报配置")
    @PreAuthorize("@sys.hasPermission('sys:org-config-placard:update')")
    public CommonResult<Boolean> updatePlacard(@Valid @RequestBody SysOrgConfigPlacardUpdateReq updateReqVO) {
        sysOrgConfigService.updatePlacard(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/placard/delete")
    @Operation(summary = "删除机构配置信息-海报配置")
    @Parameter(name = "orgConfigPlacardId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@sys.hasPermission('sys:org-config-placard:delete')")
    public CommonResult<Boolean> deletePlacard(String orgConfigPlacardId) {
        sysOrgConfigService.deletePlacardById(orgConfigPlacardId);
        return success(true);
    }

    @GetMapping("/placard/page")
    @Operation(summary = "获得机构配置信息-海报配置分页列表")
    @Parameter(name = "orgConfigId", description = "基本配置信息ID", required = true, example = "1024")
    @PreAuthorize("@sys.hasPermission('sys:org-config-placard:query')")
    public CommonResult<List<SysOrgConfigPlacardResp>> pagePlacards(@RequestParam("orgConfigId") String orgConfigId) {
        List<SysOrgConfigPlacardResp> pageResult = sysOrgConfigService.listPlacardsByOrgConfigId(orgConfigId);
        return success(pageResult);
    }
    @GetMapping("/placard/get")
    @Operation(summary = "获取机构配置信息-海报配置信息")
    @PreAuthorize("@sys.hasPermission('sys:org-config-placard:query')")
    @Parameters({
            @Parameter(name = "orgConfigId", description = "基本配置信息ID", required = true, example = "1024"),
            @Parameter(name = "orgConfigPlacardId", description = "海报配置信息ID", required = true, example = "1024")
    })
    public CommonResult<SysOrgConfigPlacardResp> getPlacard(
            @RequestParam("orgConfigId") String orgConfigId,
            @RequestParam("orgConfigPlacardId") String orgConfigPlacardId) {
        return success(sysOrgConfigService.getByPlacardId(orgConfigId,orgConfigPlacardId));
    }

    @PostMapping("/links/create")
    @Operation(summary = "创建机构配置信息-友情链接配置")
    @PreAuthorize("@sys.hasPermission('sys:org-config-links:create')")
    public CommonResult<String> createLinks(@Valid @RequestBody SysOrgConfigLinksReq createReqVO) {
        String orgConfigLinksId = sysOrgConfigService.createLinks(createReqVO);
        return success(orgConfigLinksId);
    }

    @PutMapping("/links/update")
    @Operation(summary = "修改机构配置信息-友情链接配置")
    @PreAuthorize("@sys.hasPermission('sys:org-config-links:update')")
    public CommonResult<Boolean> updateLinks(@Valid @RequestBody SysOrgConfigLinksUpdateReq updateReqVO) {
        sysOrgConfigService.updateLinks(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/links/delete")
    @Operation(summary = "删除机构配置信息-友情链接配置")
    @Parameter(name = "orgConfigLinksId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@sys.hasPermission('sys:org-config-links:delete')")
    public CommonResult<Boolean> deleteLinks(String orgConfigLinksId) {
        sysOrgConfigService.deleteLinksById(orgConfigLinksId);
        return success(true);
    }

    @GetMapping("/links/page")
    @Operation(summary = "获得机构配置信息-友情链接配置分页列表")
    @Parameter(name = "orgConfigId", description = "基本配置信息ID", required = true, example = "1024")
    @PreAuthorize("@sys.hasPermission('sys:org-config-links:query')")
    public CommonResult<List<SysOrgConfigLinksResp>> pageLinks(@RequestParam("orgConfigId") String orgConfigId) {
        List<SysOrgConfigLinksResp> pageResult = sysOrgConfigService.listLinksByOrgConfigId(orgConfigId);
        pageResult.sort(Comparator.comparing(SysOrgConfigLinksResp::getLinksType).thenComparing(SysOrgConfigLinksResp::getLinksSort));
        return success(pageResult);
    }
    @GetMapping("/links/get")
    @Operation(summary = "获取机构配置信息-友情链接配置信息")
    @PreAuthorize("@sys.hasPermission('sys:org-config-links:query')")
    @Parameters({
            @Parameter(name = "orgConfigId", description = "基本配置信息ID", required = true, example = "1024"),
            @Parameter(name = "orgConfigLinksId", description = "海报配置信息ID", required = true, example = "1024")
    })
    public CommonResult<SysOrgConfigLinksResp> getLink(
            @RequestParam("orgConfigId") String orgConfigId,
            @RequestParam("orgConfigLinksId") String orgConfigLinksId) {
        return success(sysOrgConfigService.getByLinksId(orgConfigId,orgConfigLinksId));
    }
    @PostMapping("/guidance/create")
    @Operation(summary = "创建机构配置信息-导航栏配置")
    @PreAuthorize("@sys.hasPermission('sys:org-config-guidance:create')")
    public CommonResult<String> createGuidance(@Valid @RequestBody SysOrgConfigGuidanceReq createReqVO) {
        String orgConfigGuidanceId = sysOrgConfigService.createGuidance(createReqVO);
        return success(orgConfigGuidanceId);
    }

    @PutMapping("/guidance/update")
    @Operation(summary = "修改机构配置信息-导航栏配置")
    @PreAuthorize("@sys.hasPermission('sys:org-config-guidance:update')")
    public CommonResult<Boolean> updateGuidance(@Valid @RequestBody SysOrgConfigGuidanceUpdateReq updateReqVO) {
        sysOrgConfigService.updateGuidance(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/guidance/delete")
    @Operation(summary = "删除机构配置信息-导航栏配置")
    @Parameter(name = "orgConfigGuidanceId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@sys.hasPermission('sys:org-config-guidance:delete')")
    public CommonResult<Boolean> deleteGuidance(String orgConfigGuidanceId) {
        sysOrgConfigService.deleteGuidanceById(orgConfigGuidanceId);
        return success(true);
    }

    @GetMapping("/guidance/page")
    @Operation(summary = "获得机构配置信息-导航栏配置分页列表")
    @Parameter(name = "orgConfigId", description = "基本配置信息ID", required = true, example = "1024")
    @PreAuthorize("@sys.hasPermission('sys:org-config-guidance:query')")
    public CommonResult<List<SysOrgConfigGuidanceResp>> pageGuidances(@RequestParam("orgConfigId") String orgConfigId) {
        List<SysOrgConfigGuidanceResp> pageResult = sysOrgConfigService.listGuidancesByOrgConfigId(orgConfigId);
        return success(pageResult);
    }
    @GetMapping("/guidance/get")
    @Operation(summary = "获取机构配置信息-导航栏配置信息")
    @PreAuthorize("@sys.hasPermission('sys:org-config-guidance:query')")
    @Parameters({
            @Parameter(name = "orgConfigId", description = "基本配置信息ID", required = true, example = "1024"),
            @Parameter(name = "orgConfigGuidanceId", description = "海报配置信息ID", required = true, example = "1024")
    })
    public CommonResult<SysOrgConfigGuidanceResp> getGuidance(
            @RequestParam("orgConfigId") String orgConfigId,
            @RequestParam("orgConfigGuidanceId") String orgConfigGuidanceId) {
        return success(sysOrgConfigService.getByGuidanceId(orgConfigId,orgConfigGuidanceId));
    }

    @GetMapping("/details")
    @Operation(summary = "获取机构通用配置全部信息")
    @PermitAll
    public CommonResult<SysOrgConfigAllResp> details() {
        return success(sysOrgConfigService.getAllDetails());
    }

}
